package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.company.*;
import com.nanyang.tms.api.dto.response.company.CompanyDetailResponse;
import com.nanyang.tms.api.dto.response.company.CompanyPageKeyResponse;
import com.nanyang.tms.api.dto.response.company.CompanyPageResponse;
import com.nanyang.tms.api.dto.response.company.CompanyResponse;
import com.nanyang.tms.api.service.company.CompanyRpcService;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.dao.company.CompanyDao;
import com.nanyang.tms.domain.company.CompanyDO;
import com.nanyang.tms.domain.company.CompanyQuery;
import com.nanyang.tms.domain.company.CompanyVO;
import com.nanyang.tms.manager.company.CompanyManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyRpcServiceImpl implements CompanyRpcService {

    @Autowired
    private CompanyManager companyManager;
    @Autowired
    private CompanyDao companyDao;
    @Override
    public ApiResult<PageBean<CompanyPageResponse>> listCompany(CompanyQueryRequest param) {
        ApiResult<PageBean<CompanyPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyQuery query = new CompanyQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyVO> companyList = companyManager.listCompany(query);
            PageBean<CompanyPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyList.getTotalItem());
            BeanUtils.copyListProperties(companyList.getData(), resultList.getData(), CompanyPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<CompanyPageKeyResponse>> listCompanyByKey(CompanyQueryRequest param) {
        ApiResult<PageBean<CompanyPageKeyResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            param.setState(0);//生效
            CompanyQuery query = new CompanyQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyVO> companyList = companyManager.listCompany(query);
            PageBean<CompanyPageKeyResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyList.getTotalItem());
            BeanUtils.copyListProperties(companyList.getData(), resultList.getData(), CompanyPageKeyResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<CompanyDetailResponse> detail(String code) {
        ApiResult<CompanyDetailResponse> result = new ApiResult<>();
        try {
            CompanyDetailResponse companyResult = new CompanyDetailResponse();
            CompanyVO companyVo = companyDao.selectByCode(code);
            if(Objects.isNull(companyVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(companyVo,  companyResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( companyResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<CompanyResponse> selectByCode(String code) {
        ApiResult<CompanyResponse> result = new ApiResult<>();
        try {
            CompanyResponse companyResult = new CompanyResponse();
            CompanyVO companyVo = companyDao.selectByCode(code);
            if(Objects.isNull(companyVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(companyVo,  companyResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( companyResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="companyDisable")
    @Override
    public ApiResult<Boolean> disableState(CompanyUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDo = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDo);
            companyDo.setGmtModifiedPin(param.getPin());
            companyManager.disableState(companyDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="companyEnable")
    @Override
    public ApiResult<Boolean> enableState(CompanyUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDo = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDo);
            companyDo.setGmtModifiedPin(param.getPin());
            companyManager.enableState(companyDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="companyPass")
    @Override
    public ApiResult<Boolean> passState(CompanyUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDO = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDO);
            companyDO.setGmtModifiedPin(param.getPin());
            companyManager.passState(companyDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="companyReject")
    @Override
    public ApiResult<Boolean> rejectState(CompanyUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDO = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDO);
            companyDO.setGmtModifiedPin(param.getPin());
            if(companyDao.rejectState(companyDO)<=0){
                throw new TmsManagerException(TmsExceptionEnum.COMPANY_STATE_ERROR.getCode(),TmsExceptionEnum.COMPANY_STATE_ERROR.getMessage());
            };
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveCompany(CompanyAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyVO companyVO = new  CompanyVO();
            BeanUtils.copyProperties(param,companyVO);
            companyVO.setGmtCreatePin(param.getPin());
            companyManager.saveCompany(companyVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateCompany(CompanyUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDO = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDO);
            companyDO.setGmtModifiedPin(param.getPin());
            companyDO.setGmtModified(new Date());
            companyManager.updateCompany(companyDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemMessage(messageType=SystemMessage.MessageType.SERVICE,template="companyLevelMsg")
    @SystemLog(module =SystemLog.Module.COMPANY,template="companyLevel")
    @Override
    public ApiResult<Boolean> updateCompanyLevel(CompanyUpdateLevelRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDO companyDO = new  CompanyDO();
            BeanUtils.copyProperties(param,companyDO);
            companyDO.setGmtModifiedPin(param.getPin());
            companyDO.setGmtModified(new Date());
            companyDao.update(companyDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
